﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Areas.Administrator.Models;

namespace CollegeWebsite.Areas.Administrator.Controllers
{
    public class StudentController : Controller
    {
        //
        // GET: /Administrator/Student/
        CollegeWebsiteDataContext _datacontext = new CollegeWebsiteDataContext();
        public ActionResult Index()
        {
            List<Student> stu = _datacontext.Students.Select(s=>s).ToList();
            return View(stu);


        }

        //
        // GET: /Administrator/Student/Details/5

        public ActionResult Details(int id)
        {
            Student stu = _datacontext.Students.Select(s => s).Single(s => (s.StudentID == id));
            return View(stu);
        }

        //
        // GET: /Administrator/Student/Create

        public ActionResult Create()
        {
            List<Course> cou = _datacontext.Courses.Select(c => c).Where(c => (c.Status == true)).ToList();
            ViewData["Course"] = cou;
            Student stu = new Student();
            stu.Gender = true;
            stu.Status = true;
            return View(stu);
        } 

        //
        // POST: /Administrator/Student/Create

        [HttpPost]
        public ActionResult Create(Student stu, FormCollection formCollection)
        {
            try
            {
             // stu.Admission.Course.CourseID = Convert.ToInt32(formCollection.Get("CourseID"));
             // stu.Admission.FacultyID = stu.Admission.Course.FacultyID;
             // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    _datacontext.Students.InsertOnSubmit(stu);
                    _datacontext.SubmitChanges();
                    return RedirectToAction("Index"); 
                }
                return View();
            }
            catch(Exception exc)
            {
                ViewData["ErrorInfo"] = exc.Message;
                return View("Error");
            }
        }
        
        //
        // GET: /Administrator/Student/Edit/5
 
        public ActionResult Edit(int id)
        {
            List<Course> cou = _datacontext.Courses.Select(c => c).Where(c => (c.Status == true)).ToList();
            ViewData["Course"] = cou;
            Student stu = _datacontext.Students.Single(s => s.StudentID == id);
            return View(stu);
        }

        //
        // POST: /Administrator/Student/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Student newstu)
        {
            try
            {
                // TODO: Add update logic here
                Student oldstu = _datacontext.Students.Single(s => s.StudentID == id);
                oldstu.StudentName = newstu.StudentName;
                oldstu.FatherName = newstu.FatherName;
                oldstu.MotherName = newstu.MotherName;
                oldstu.Gender = newstu.Gender;
               // oldstu.AdmissionID = newstu.AdmissionID;
                oldstu.SupportDetails = newstu.SupportDetails;
                oldstu.Status = newstu.Status;

                _datacontext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch( Exception exception)
            {
                ViewData["ErrorInfo"] = exception.StackTrace;
                return View("Error");
            }
        }

        //
        // GET: /Administrator/Student/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Administrator/Student/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
